package com.lltt.study.controller;

import cn.hutool.core.date.DateUtil;
import com.lltt.study.entities.LawPrompt;
import com.lltt.study.service.LawAssistant;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.chat.response.ChatResponse;
import dev.langchain4j.model.input.Prompt;
import dev.langchain4j.model.input.PromptTemplate;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@Slf4j
public class ChatPromptController {
    @Resource
    private LawAssistant lawAssistant;

    @GetMapping(value = "/chatprompt/test1")
    public String test1(){
        String chat = lawAssistant.chat("什么是知识产权？",2000);
        System.out.println(chat);

        String chat2 = lawAssistant.chat("什么是java？",2000);
        System.out.println(chat2);

        String chat3 = lawAssistant.chat("介绍下西瓜和芒果",2000);
        System.out.println(chat3);

        String chat4 = lawAssistant.chat("飞机发动机原理",2000);
        System.out.println(chat4);

        return "success : "+ DateUtil.now()+"<br> \n\n chat: "+chat+"<br> \n\n chat2: "+chat2;
    }
    @GetMapping(value = "/chatprompt/test2")
    public String test2(){
        LawPrompt lawPrompt = new LawPrompt();
        lawPrompt.setLegal("知识产权");
        lawPrompt.setQuestion("TRIPS协议?");
        String chat = lawAssistant.chat(lawPrompt);
        System.out.println(chat);
        return "success : "+ DateUtil.now()+"<br> \n\n chat: "+chat;
    }
    @GetMapping(value = "/chatprompt/test3")
    public String test3(ChatModel chatModel){
        // 默认 PromptTemplate 构造使用 it 属性作为默认占位符
        String role = "外科医生";
        String question ="牙疼";
        /*String role = "财务会计";
        String question = "人民币大写";*/

        //1.构造PromptTemplate模板
        PromptTemplate template = PromptTemplate.from("你是一个{{it}}助手,{{question}}怎么办");
        //2.由PromptTemplate生成Prompt
        Prompt prompt = template.apply(Map.of("it", role, "question", question));
        //3.prompt提示词变为UserMessage
        UserMessage userMessage = prompt.toUserMessage();
        //4.调用大模型
        ChatResponse chatResponse = chatModel.chat(userMessage);

        //4.1后台打印
        System.out.println(chatResponse.aiMessage().text());
        //4.2前台打印
        return "success:" + DateUtil.now() +"<br> \n \n  chat:" + chatResponse.aiMessage().text();

    }
}
